package wang.ray.sample.ruoyi.cms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import wang.ray.sample.ruoyi.cms.domain.Blog;
import wang.ray.sample.ruoyi.cms.service.IBlogService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;

/**
 * 博客 信息操作处理
 * 
 * @author Ray
 * @date 2018-11-16
 */
@Controller
@RequestMapping("/cms/blog")
public class BlogController extends BaseController
{
    private String prefix = "cms/blog";
	
	@Autowired
	private IBlogService blogService;
	
	@RequiresPermissions("cms:blog:view")
	@GetMapping()
	public String blog()
	{
	    return prefix + "/blog";
	}
	
	/**
	 * 查询博客列表
	 */
	@RequiresPermissions("cms:blog:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Blog blog)
	{
		System.out.println("***************");
		startPage();
        List<Blog> list = blogService.selectBlogList(blog);
		return getDataTable(list);
	}
	
	/**
	 * 新增博客
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存博客
	 */
	@RequiresPermissions("cms:blog:add")
	@Log(title = "博客", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Blog blog)
	{		
		return toAjax(blogService.insertBlog(blog));
	}

	/**
	 * 修改博客
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Blog blog = blogService.selectBlogById(id);
		mmap.put("blog", blog);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存博客
	 */
	@RequiresPermissions("cms:blog:edit")
	@Log(title = "博客", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Blog blog)
	{		
		return toAjax(blogService.updateBlog(blog));
	}
	
	/**
	 * 删除博客
	 */
	@RequiresPermissions("cms:blog:remove")
	@Log(title = "博客", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(blogService.deleteBlogByIds(ids));
	}
	
}
